package com.hsy.linemoney.modules.account;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.AccountService;
import com.hsy.linemoney.apiservice.BankService;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.apiservice.PaymentService;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.modules.common.EditTextExt;
import com.hsy.linemoney.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CreateSendAccountActivity extends CommonBaseActivityWithSimpleActionBar implements View.OnClickListener {

    protected TextView selectPaymentTypeSpinner, nextStep;
    private RelativeLayout changeable_area;

    private String mPaymentTypeCd;

    private EditTextExt accountName;

    private EditTextExt accountHolderName;
    private TextView selectBankNameSpinner;
    private EditTextExt bankAccount;
    private EditTextExt bankAccountAgain;
    private int selectedBankNameWhich = -1;

    private EditTextExt cardHolderName;
    private EditTextExt cardNumber;
    private EditTextExt deadLine;
    private EditTextExt securityCode;
    private EditTextExt accountPass;
    private EditTextExt accountPassAgain;

    private EditTextExt zhifubaoAccount;
    private EditTextExt zhifubaoAccountAgain;
    private BankService.BankListResult mBankListResult;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_add_send_account);
        setTitle(R.string.txt_add_account);
        initView();
    }

    private void initView() {
        selectPaymentTypeSpinner = (TextView) findViewById(R.id.select_payment_type_spinner);
        selectPaymentTypeSpinner.setOnClickListener(this);
        nextStep = (TextView) findViewById(R.id.next_step);
        nextStep.setOnClickListener(CreateSendAccountActivity.this);
        changeable_area = (RelativeLayout) findViewById(R.id.changeable_area);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.select_payment_type_spinner) {
            new PaymentService().getPaymentType(new Callback<PaymentService.PaymentTypeListResult>() {
                @Override
                public void onSuccess(int what, final PaymentService.PaymentTypeListResult result) {
                    CommonUtils.showListDialog(CreateSendAccountActivity.this, result.getPaymentNameArray(), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            CreateSendAccountActivity.this.mPaymentTypeCd = result.getValue().get(which).getPaymentTypeCd();
                            selectPaymentTypeSpinner.setText(result.getValue().get(which).getName());

                            if ("bank".equalsIgnoreCase(mPaymentTypeCd)) {
                                changeable_area.removeAllViewsInLayout();
                                View v = inflate(R.layout.create_send_account_for_bank_layout);
                                changeable_area.addView(v, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                                accountName = (EditTextExt) findViewById(R.id.account_name);
                                accountHolderName = (EditTextExt) findViewById(R.id.account_holder_name);
                                selectBankNameSpinner = (TextView) findViewById(R.id.select_bank_name_spinner);
                                bankAccount = (EditTextExt) findViewById(R.id.bank_account);
                                bankAccountAgain = (EditTextExt) findViewById(R.id.bank_account_again);

                                selectedBankNameWhich = -1;
                                selectBankNameSpinner.setOnClickListener(CreateSendAccountActivity.this);

                            } else if ("card".equalsIgnoreCase(mPaymentTypeCd)) {
                                changeable_area.removeAllViewsInLayout();
                                View v = inflate(R.layout.create_send_account_for_card_layout);
                                changeable_area.addView(v, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                                accountName = (EditTextExt) findViewById(R.id.account_name);
                                cardHolderName = (EditTextExt) findViewById(R.id.card_holder_name);
                                cardNumber = (EditTextExt) findViewById(R.id.card_number);
                                deadLine = (EditTextExt) findViewById(R.id.dead_line);
                                securityCode = (EditTextExt) findViewById(R.id.security_code);
                                accountPass = (EditTextExt) findViewById(R.id.account_pass);
                                accountPassAgain = (EditTextExt) findViewById(R.id.account_pass_again);


                            } else if ("zhiFuBao".equalsIgnoreCase(mPaymentTypeCd)) {
                                changeable_area.removeAllViewsInLayout();
                                View v = inflate(R.layout.create_send_account_for_zhifubao_layout);
                                changeable_area.addView(v, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                                accountName = (EditTextExt) findViewById(R.id.account_name);
                                zhifubaoAccount = (EditTextExt) findViewById(R.id.zhifubao_account);
                                zhifubaoAccountAgain = (EditTextExt) findViewById(R.id.zhifubao_account_again);

                            } else if ("paypal".equalsIgnoreCase(mPaymentTypeCd)) {
                                changeable_area.removeAllViewsInLayout();
                                View v = inflate(R.layout.create_send_account_for_paypal_layout);
                                changeable_area.addView(v, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                                accountName = (EditTextExt) v.findViewById(R.id.account_name);
                            }
                        }
                    });
                }

                @Override
                public void onError(int what, int type, String msg, Exception e) {

                }
            });
        } else if (view.getId() == R.id.next_step) {
            if ("bank".equalsIgnoreCase(mPaymentTypeCd)) {
                createBankAccount();
            } else if ("card".equalsIgnoreCase(mPaymentTypeCd)) {
                createCardAccount();
            } else if ("zhiFuBao".equalsIgnoreCase(mPaymentTypeCd)) {
                createZhifubaoAccount();
            } else if ("paypal".equalsIgnoreCase(mPaymentTypeCd)) {
                createPaypayAccount();
            }
        } else if (view.getId() == R.id.select_bank_name_spinner) {
            BankService bankService = new BankService();
            bankService.getBankList(new Callback<BankService.BankListResult>() {
                @Override
                public void onSuccess(final int what, final BankService.BankListResult result) {
                    CreateSendAccountActivity.this.mBankListResult = result;
                    CommonUtils.showListDialog(CreateSendAccountActivity.this, result.getBankNameArray(), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            selectedBankNameWhich = which;
                            selectBankNameSpinner.setText(result.getBankNameArray()[which]);
                        }
                    });
                }

                @Override
                public void onError(int what, int type, String msg, Exception e) {

                }
            });
        }
    }

    private void createBankAccount() {
        if (checkThroughtBank()) {
            showProgressDialog(this);
            AccountService service = new AccountService();
            CreateAccountAndHolderParamsModel p = new CreateAccountAndHolderParamsModel();
            p.setAccountTypeCd("SEND");
            p.setPaymentTypeCd(mPaymentTypeCd);
            p.setCurrencyCd(getActivityStartExtra()[0]);

            p.setAccountName(accountName.getText().toString());
            p.setFullName(accountHolderName.getText().toString());
            p.setBankCd(mBankListResult.getValue().get(selectedBankNameWhich).getBankCd());
            p.setBankAccount(bankAccount.getText().toString());

            try {
                JSONObject params = new JSONObject(DPGsonUtil.toJson(p));
                service.createAccountAndHolder(params, new Callback<JSONArray>() {
                    @Override
                    public void onSuccess(int what, JSONArray result) {
                        AccountService service = new AccountService();
                        service.getAccounts(new Callback<AccountService.AccountListResult>() {
                            @Override
                            public void onSuccess(int what, AccountService.AccountListResult result) {
                                finish();
                                hideProgressDialog();
                            }

                            @Override
                            public void onError(int what, int type, String msg, Exception e) {
                                finish();
                                hideProgressDialog();
                            }
                        }, true);
                    }

                    @Override
                    public void onError(int what, int type, String msg, Exception e) {
                        showToast(R.string.txt_account_add_failure);
                        e.printStackTrace();
                        hideProgressDialog();
                    }
                });
            } catch (JSONException e) {
                showToast(R.string.txt_account_add_failure);
                e.printStackTrace();
                hideProgressDialog();
            }
        }
    }

    private boolean checkThroughtBank() {
        if (TextUtils.isEmpty(accountName.getText().toString())) {
            showToast(R.string.txt_account_name_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(accountHolderName.getText().toString())) {
            showToast(R.string.txt_account_holder_name_cannot_be_empty);
            return false;
        }
        if (selectedBankNameWhich < 0) {
            showToast(R.string.txt_please_select_bank_name);
            return false;
        }
        if (TextUtils.isEmpty(bankAccount.getText().toString())) {
            showToast(R.string.txt_bank_account_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(bankAccountAgain.getText().toString())) {
            showToast(R.string.txt_please_fill_bank_account_again);
            return false;
        }
        if (!bankAccount.getText().toString().equals(bankAccountAgain.getText().toString())) {
            showToast(R.string.txt_please_bank_not_match);
            return false;
        }
        return true;
    }

    private void createCardAccount() {
        if (checkThroughtCard()) {
            showProgressDialog(this);
            AccountService service = new AccountService();
            CreateAccountAndHolderParamsModel p = new CreateAccountAndHolderParamsModel();
            p.setAccountTypeCd("SEND");
            p.setPaymentTypeCd(mPaymentTypeCd);
            p.setCurrencyCd(getActivityStartExtra()[0]);

            p.setAccountName(accountName.getText().toString());
            p.setFullName(cardHolderName.getText().toString());
            p.setCardNumber(cardNumber.getText().toString());
            p.setExpirationDate(deadLine.getText().toString());
            p.setSecurityCode(securityCode.getText().toString());
            p.setPassword(accountPass.getText().toString());

            try {
                JSONObject params = new JSONObject(DPGsonUtil.toJson(p));
                service.createAccountAndHolder(params, new Callback<JSONArray>() {
                    @Override
                    public void onSuccess(int what, JSONArray result) {
                        AccountService service = new AccountService();
                        service.getAccounts(new Callback<AccountService.AccountListResult>() {
                            @Override
                            public void onSuccess(int what, AccountService.AccountListResult result) {
                                finish();
                                hideProgressDialog();
                            }

                            @Override
                            public void onError(int what, int type, String msg, Exception e) {
                                finish();
                                hideProgressDialog();
                            }
                        }, true);
                    }

                    @Override
                    public void onError(int what, int type, String msg, Exception e) {
                        showToast(R.string.txt_account_add_failure);
                        e.printStackTrace();
                        hideProgressDialog();
                    }
                });
            } catch (JSONException e) {
                showToast(R.string.txt_account_add_failure);
                e.printStackTrace();
                hideProgressDialog();
            }
        }
    }

    private boolean checkThroughtCard() {
        if (TextUtils.isEmpty(accountName.getText().toString())) {
            showToast(R.string.txt_account_name_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(cardHolderName.getText().toString())) {
            showToast(R.string.txt_card_holder_name_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(cardNumber.getText().toString())) {
            showToast(R.string.txt_card_number_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(deadLine.getText().toString())) {
            showToast(R.string.txt_deadLine_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(securityCode.getText().toString())) {
            showToast(R.string.txt_security_code_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(accountPass.getText().toString())) {
            showToast(R.string.txt_account_pass_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(accountPassAgain.getText().toString())) {
            showToast(R.string.txt_fill_pass_again);
            return false;
        }
        if (!accountPass.getText().toString().equals(accountPassAgain.getText().toString())) {
            showToast(R.string.txt_password_not_match);
            return false;
        }
        return true;
    }

    private void createZhifubaoAccount() {
        if (checkThroughtZhifubao()) {
            showProgressDialog(this);
            AccountService service = new AccountService();
            CreateAccountAndHolderParamsModel p = new CreateAccountAndHolderParamsModel();
            p.setAccountTypeCd("SEND");
            p.setPaymentTypeCd(mPaymentTypeCd);
            p.setCurrencyCd(getActivityStartExtra()[0]);

            p.setAccountName(accountName.getText().toString());
            p.setAccountId(zhifubaoAccount.getText().toString());

            try {
                JSONObject params = new JSONObject(DPGsonUtil.toJson(p));
                service.createAccountAndHolder(params, new Callback<JSONArray>() {
                    @Override
                    public void onSuccess(int what, JSONArray result) {
                        AccountService service = new AccountService();
                        service.getAccounts(new Callback<AccountService.AccountListResult>() {
                            @Override
                            public void onSuccess(int what, AccountService.AccountListResult result) {
                                finish();
                                hideProgressDialog();
                            }

                            @Override
                            public void onError(int what, int type, String msg, Exception e) {
                                finish();
                                hideProgressDialog();
                            }
                        }, true);
                    }

                    @Override
                    public void onError(int what, int type, String msg, Exception e) {
                        showToast(R.string.txt_account_add_failure);
                        e.printStackTrace();
                        hideProgressDialog();
                    }
                });
            } catch (JSONException e) {
                showToast(R.string.txt_account_add_failure);
                e.printStackTrace();
                hideProgressDialog();
            }
        }
    }

    //    accountName = (EditTextExt) findViewById(R.id.account_name);
//    zhifubaoAccount = (EditTextExt) findViewById(R.id.zhifubao_account);
//    zhifubaoAccountAgain = (EditTextExt) findViewById(R.id.zhifubao_account_again);
    private boolean checkThroughtZhifubao() {
        if (TextUtils.isEmpty(accountName.getText().toString())) {
            showToast(R.string.txt_account_name_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(zhifubaoAccount.getText().toString())) {
            showToast(R.string.txt_zhifubao_account_cannot_be_empty);
            return false;
        }
        if (TextUtils.isEmpty(zhifubaoAccountAgain.getText().toString())) {
            showToast(R.string.txt_fill_zhifubao_account_again);
            return false;
        }
        if (!zhifubaoAccount.getText().toString().equals(zhifubaoAccountAgain.getText().toString())) {
            showToast(R.string.txt_account_name_cannot_be_empty);
            return false;
        }
        return true;
    }

    private void createPaypayAccount() {
        if (checkThroughtPaypal()) {
            showProgressDialog(this);
            AccountService service = new AccountService();
            CreateAccountAndHolderParamsModel p = new CreateAccountAndHolderParamsModel();
            p.setAccountName(accountName.getText().toString());
            p.setAccountTypeCd("SEND");
            p.setPaymentTypeCd(mPaymentTypeCd);
            p.setCurrencyCd(getActivityStartExtra()[0]);
            try {
                JSONObject params = new JSONObject(DPGsonUtil.toJson(p));
                service.createAccountAndHolder(params, new Callback<JSONArray>() {
                    @Override
                    public void onSuccess(int what, JSONArray result) {
                        AccountService service = new AccountService();
                        service.getAccounts(new Callback<AccountService.AccountListResult>() {
                            @Override
                            public void onSuccess(int what, AccountService.AccountListResult result) {
                                finish();
                                hideProgressDialog();
                            }

                            @Override
                            public void onError(int what, int type, String msg, Exception e) {
                                finish();
                                hideProgressDialog();
                            }
                        }, true);
                    }

                    @Override
                    public void onError(int what, int type, String msg, Exception e) {
                        showToast(R.string.txt_account_add_failure);
                        e.printStackTrace();
                        hideProgressDialog();
                    }
                });
            } catch (JSONException e) {
                showToast(R.string.txt_account_add_failure);
                e.printStackTrace();
                hideProgressDialog();
            }
        }
    }

    private boolean checkThroughtPaypal() {
        if (TextUtils.isEmpty(accountName.getText().toString())) {
            showToast(R.string.txt_account_name_cannot_be_empty);
            return false;
        }
        return true;
    }

}
